package com.itheima;

import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;

public class MessageProducer {

    public static void main(String[] args) throws Exception {
        //1.创建连接工厂ConnectionFactoty
        ActiveMQConnectionFactory connectionFactory= new ActiveMQConnectionFactory("tcp://192.168.144.100:61616");
        //2. 通过工厂获取连接Connection,并开启连接
        Connection connection = connectionFactory.createConnection();
        connection.start();
        //3.通过连接创建会话Seesion
                //参数一  false: 代表是否需要事务, 参数二 :代表应答机制
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //4.创建目的地 Queue 和Topic  destination(目的地)
        //Queue 的发送的消息,不会丢失   Topic 要先启动消费者接受,生产者产生消息,消费者才能接收到消息
        Queue queue = session.createQueue("queue");

        Topic topic = session.createTopic("topic");

        //5.创建消息生产者
        javax.jms.MessageProducer producer = session.createProducer(topic);
        //6.创建消息对象并设置消息信息
        TextMessage textMessage = session.createTextMessage();
        textMessage.setText("来了,queue");

        //7.发送消息
        producer.send(textMessage);

        //8.关闭资源
        session.close();
        connection.start();



    }

}
